import {uploadUrl} from "@/settings.js";

const uploadUrl = uploadUrl;
const exclude = ['/login', '/captchaImage']; // 排除token验证

export default function upload(tempFilePath) {
	return new Promise(resolve => {
		let header = {};
		let token = uni.getStorageSync('token');
		if (token == '') {
			resolve({
				data: {
					code: 401,
					msg: 'token=null'
				}
			});
			goLogin();
			return;
		} else {
			header['Authorization'] = 'Bearer ' + token;
		}

		console.log('tempFilePath',tempFilePath)

		uni.uploadFile({
			url: uploadUrl,
			filePath: tempFilePath,
			name: 'file',
			formData: {
			},
			header: header,
			fail(res) {
				console.log(res)
			},
			complete(res) {
				// 未登录
				if (res.code && res.data && (res.code == 401 || res.data.code == 401)) {
					goLogin();
				}else{
					resolve(res);
				}
			}
		})
	})
}

function goLogin() {
	uni.showToast({
		title: '登录失效',
		icon: 'none'
	})
	uni.removeStorageSync('token');
	setTimeout(() => {
		console.log('去登录');
		uni.reLaunch({
			url: '/pages/login/login'
		})
	}, 1000)
}
